//
//  EmoticonPackage.swift
//  阳光表情键盘
//
//  Created by 阿拉斯加的狗 on 15/12/15.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {

    //表情包所在路径
    var id: String?
    //表情包得名称
    var group_name_cn: String?
    //表情数组
    lazy var emoticons = [Emoticon]()
    
    
    //字典转模型
    init(dict: [String: AnyObject]) {
    
        super.init()
        
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        //1.获取字典的数组
        if let array = dict["emoticons"] as? [[String: AnyObject]] {
        
            var index = 0
            //遍历数组
            for var d in array {
                
                //判断是否有png值
                if let png = d["png"] as? String, dir = id {
                
                    //从新设置字典的png的值
                    d["png"] = dir + "/" + png
                    
                }
            
                emoticons.append(Emoticon(dict: d))
                
                //每隔20个就添加一个删除按钮
                index++
                
                if index == 20 {
                
                    emoticons.append(Emoticon(isRemoved: true))
                    
                    index = 0
                }
            
            }
        }
        // 添加空白按钮
        appendEmptyEmoticon()
        
    }
    
    //在表情数组末尾,添加空白按钮
    private func appendEmptyEmoticon(){
    
        //获取表情的余数
        let count = emoticons.count % 21
        
        //判断
        if emoticons.count > 0 && count == 0 {
        
            return
        }
    
        
        //添加空白表情
        for _ in count..<20 {
        
            emoticons.append(Emoticon(isEmpty: true))
        }
        
        //末尾添加一个删除按钮
        emoticons.append(Emoticon(isRemoved: true))
    }
    
    //返回信息
    override var description: String {
        let keys = ["id", "group_name_cn", "emoticons"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
